<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>手写promise</title>
</head>
<body>
<ol>
    <li>实现一个promise，在 setTimeout 中去 resolve</li>
    <li>实现一个promise，直接同步 resolve</li>
</ol>
<script>
    class MyPromise{
        constructor(fn) {
            this.resolveCallbacks = [];

            const resolve = val => {
                // 这里有宏任务和微任务的知识
                setTimeout(() => {
                    this.resolveCallbacks.map(fn => fn(val));
                })
            }

            const reject = () => {

            }

            fn(resolve,reject);
        }

        then(onFulfilled){
            this.resolveCallbacks.push(onFulfilled);
        }
    }

    new MyPromise((resolve,reject) => {
        resolve('step2');
    }).then(res => {
        console.log(res);
    });
</script>
</body>
</html>